var link='';
var count=1;
$(document).ready(function() {
    //getLanguages();
    var langid=$('#languageholder').attr('langid');
    $('#language').val(langid);
});

function getLanguages(){
    $.ajax({
        type: 'POST',
        url: '../../ajax/getlanguages',
        data: {},
        success: getlangsuccess
    });
}

var getlangsuccess=function getlangsuccess(data){
    //alert(data);
    $('#languageholder').html(data);
}

function createsubmission(){
    
    var data='';
    var code=editor.getValue();
    var input=$('#inputtextarea').val();
    var languageid=$('#language option:selected').val();
    var logic=$('#logic').val();
    data={
        'code': code,
        'input':input,
        'languageid': languageid,
        'logic':logic
    };
    
    $('#runstatus').html('Uploading');
    
    $.ajax({
        type: 'POST',
        url: '../../ajax/createsubmission',
        data: data,
        success: createsubmissionsuccess,
        dataType: 'json'
    });
}


function runcode(){
    
    var data='';
    var code=editor.getValue();
    var input=$('#inputtextarea').val();
    var languageid=11;
    data={
        'code': code,
        'input':input,
        'languageid': languageid
    };
    
   
    
    $.ajax({
        type: 'POST',
        url: '../../ajax/runcode',
        data: data,
        success: runsuccess,
        dataType: 'json'
    });
}

var createsubmissionsuccess=function success(data){
    // alert('Result.link: '+data.result.link);
   
    if(data!=null && data.result.error != 'OK'){
        alert('submission failed!!');
        return false;
    }
   
    link=data.result.link;
    getsubmissionstatus();
}

function getsubmissionstatus(){
    data={
        'link':link
    };
    
   
    
    $.ajax({
        type: 'POST',
        url: '../../ajax/getsubmissionstatus',
        data: data,
        success: getsubmissionstatussuccess,
        dataType: 'json'
    });
}

function getsubmissiondetails(){
    data={
        'link':link
    };
    
   
    
    $.ajax({
        type: 'POST',
        url: '../../ajax/getsubmissiondetails',
        data: data,
        success: getsubmissiondetailssuccess,
        dataType: 'json'
    });
}


var getsubmissionstatussuccess=function success(data){
    //alert(data.status.status);
    
    if(data==null || (data!=null && data.status.error != 'OK')){
        alert('submission status check failed!!');
        return false;
    }
    
    var statuscode=data.status.status;
    var status=getstatus(data.status.status);
    
    $('#runstatus').html(status);
    //count=count+1;
    //alert(status);
    if(statuscode!=0){
        setTimeout( function() {
            getsubmissionstatus();
        }, 3 );
    }
    else{
        $('#runstatus').html('');
        var result=getstatusresult(data.status.result);
        $('#runresult').html(result);
        getsubmissiondetails();
    }
    
}

var getsubmissiondetailssuccess=function success(data){

    if(data==null){
        alert('submission details check failed!!');
        return false;
    }

    $('#cmpinfo').html(data.details.cmpinfo);
    $('#time').html(data.details.time);
    $('#memory').html(data.details.memory);
    $('#output').html(data.details.output);
    
    
}


var runsuccess=function sucess(data){
    //alert('Result.error: '+data.details.output);
    var result=getstatusresult(data.status.result);
    
    
    $('#runresult').html(result);
    $('#time').html(data.details.time);
    $('#memory').html(data.details.memory);
    $('#output').html(data.details.output);
}

function getstatus(result){
    var status='';
    switch(result){
        case 0:
            status='Done!!';
            break
        case 1:
            status='Compiling';
            break;
        case 3:
            status='Running';
            break
        default:
            status='Submitting';
            break;
    }
    return status;
}

function getstatusresult(result){
    var status='';
    switch(result){
        case 0:
            status='Not running';
            break
        case 11:
            status='Compilation error';
            break;
        case 12:
            status='Runtime   error';
            break
        case 13:
            status='Time  limit  exceeded';
            break;
        case 15:
            status='Success';
            break
        case 17:
            status='Memory Limit Exceeded';
            break;
        case 19:
            status='Illegal system call';
            break
        case 20:
            status='Internal error';
            break;
    }
    return status;
}